package com.software.car.controller;

import com.software.car.api.ModelService;
import com.software.car.common.utils.CommonPage;
import com.software.car.common.utils.CommonResult;
import com.software.car.common.utils.SnowflakeIdGenerator;
import com.software.car.entity.dto.request.InsertModelRequest;
import com.software.car.entity.dto.response.ModelOptionResponse;
import com.software.car.entity.dto.response.ModelResponse;
import com.software.car.entity.dto.response.ModelSimpleResponse;
import com.software.car.entity.po.ModelPo;
import com.software.car.entity.po.UserPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 模板管理
 *
 * @Author 魔法はまだ解けない
 * @Date 2021/2/3
 */
@RestController
@RequestMapping("/model")
public class ModelController {

    @Autowired
    ModelService modelService;

    /**
     * 公共
     * 获取模板列表/查询        OK
     *
     * @param state
     * @param type
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/public")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult getSimpleModel(@RequestParam(name = "state", required = false) Integer state,
                                       @RequestParam(name = "type", required = false) Integer type,
                                       @RequestParam(name = "page", required = false) Integer page,
                                       @RequestParam(name = "pageSize", required = false) Integer pageSize) {

        if (page == null || pageSize == null) {
            List<ModelSimpleResponse> simpleModel = modelService.getSimpleModel(state, type);
            return CommonResult.success(simpleModel);
        }
        CommonPage commonPage = modelService.getSimpleModel(state, type, page, pageSize);
        return CommonResult.success(commonPage);
    }

    /**
     * 管理员
     * 创建模板         OK
     *
     * @param insertModelRequest
     * @param userPo
     * @return
     */
    @PostMapping("/administer")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult addModel(@RequestBody InsertModelRequest insertModelRequest,
                                 @RequestAttribute("user") UserPo userPo) {
        insertModelRequest.setId(SnowflakeIdGenerator.getId());
        insertModelRequest.setState(1);
        insertModelRequest.setCreateUser(userPo.getId());
        insertModelRequest.setCreateTime(new Date());
        insertModelRequest.setUpdateTime(new Date());
        int i = modelService.addModel(insertModelRequest);
        return CommonResult.success();
    }

    /**
     * 公共
     * 根据modelId获取模板信息      OK
     *
     * @param modelId
     * @return
     */
    @GetMapping("/public/optionList")
    public CommonResult getModelForm(@RequestParam(name = "modelId") String modelId) {
        //获取状态的模板
        List<ModelOptionResponse> modelOptions = modelService.getModelOptions(modelId, 1);
        return CommonResult.success(modelOptions);
    }

    /**
     * 管理员
     * 根据modelId获取模板详细信息    OK
     *
     * @param modelId
     * @return
     */
    @GetMapping("/administer/detail")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult getModel(@RequestParam String modelId) {
        ModelResponse model = modelService.getModel(modelId);
        model.setOption(modelService.getModelOptions(modelId, null));
        return CommonResult.success(model);
    }

    /**
     * 修改模板基本信息
     *
     * @return
     */
    @PutMapping("/administer")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult updateModel(@RequestBody InsertModelRequest insertModelRequest,
                                    @RequestAttribute("user") UserPo userPo) {
        insertModelRequest.setUpdateUser(userPo.getId());
        modelService.updateModel(insertModelRequest);
        return CommonResult.success();
    }

    /**
     * 管理员
     * 修改模块状态
     *
     * @param modelId 模块id
     * @param state   状态 1正常，2删除
     * @return
     */
    @DeleteMapping("/administer")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult banModel(@RequestParam String modelId,
                                 @RequestParam Integer state,
                                 @RequestAttribute("user") UserPo userPo) {
        ModelPo modelPo = new ModelPo();
        modelPo.setId(modelId);
        modelPo.setState(state);
        modelService.updateModel(modelId, state, userPo.getId());
        return CommonResult.success();
    }

}
